package com.ckjava.test.web;

import com.ckjava.test.domain.PageParamer;
import com.ckjava.test.domain.Task;
import com.ckjava.test.service.TaskService;
import com.ckjava.test.web.base.BaseJpaCrudController;
import com.ckjava.xutils.http.HttpResponse;
import com.ckjava.xutils.http.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api
@RestController
@RequestMapping(value = "/api/task", produces = "application/json;charset=utf-8")
public class TaskController extends BaseJpaCrudController<TaskService, Task> {

    @ApiOperation("获取所有的数据 <br>")
    @GetMapping(value = "page")
    public HttpResponse<Page<Task>> page(
            @ApiParam("{\"sort\":{\"predicate\":\"createDate\",\"reverse\":true},\"search\":{\"predicateObject\":{\"alias\":\"\"}},\"pagination\":{\"start\":0,\"number\":10}}")
            @ModelAttribute PageParamer pageParamer) { // {"sort":{"predicate":"urls","reverse":true},"search":{"predicateObject":{"activity":"false"}},"pagination":{"start":10,"totalItemCount":0,"number":10,"numberOfPages":2}}) {
        try {
            Task task = new Task();

            // 排序
            task.setOrderBy(pageParamer.getSort().getPredicate());
            task.setDesc(pageParamer.getSort().isReverse());

            // 查询字段
            Map<String, String> conditionMap = new HashMap<>();
            conditionMap.put("name", "like".concat(pageParamer.getSearch().getName()));
            task.setConditionMap(conditionMap);

            // 分页导航
            task.setStart(pageParamer.getPagination().getStart());
            task.setPageSize(pageParamer.getPagination().getNumber());

            return HttpResponse.getReturn(service.getPage(task), HTTPCODE.code_200, null);
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponse.getReturn(null, HTTPCODE.code_500, null);
        }

    }
}
